<template>
  <a-spin :loading="spinLoading" :size="35" class="w-[100%]" tip="正在处理, 请稍候...">
    <a-form ref="formRef" :model="form" :rules="rules" auto-label-width layout="vertical">
      <a-form-item field="postName" label="岗位名称" required>
        <a-input v-model="form.postName" placeholder="岗位名称"/>
      </a-form-item>
      <a-form-item field="postCode" label="岗位编码" required tooltip="岗位的唯一英文标识">
        <a-input v-model="form.postCode" placeholder="岗位编码"/>
      </a-form-item>
      <a-form-item field="postSort" label="排序值" required>
        <a-input-number v-model="form.postSort" :min="0" placeholder="排序值"/>
      </a-form-item>
      <a-form-item v-if="props.params.operationType === proxy.operationType.update.type" field="status" label="岗位状态"
                   required>
        <a-select v-model="form.status" allow-clear placeholder="岗位状态">
          <a-option v-for="(d, index) in dicts[proxy.DICT.commonNumberStatus]" :key="index" :label="d.dictValue"
                    :value="d.dictKey"/>
        </a-select>
      </a-form-item>
      <a-form-item field="remark" label="备注">
        <a-textarea v-model="form.remark" placeholder="备注"/>
      </a-form-item>
    </a-form>

    <!-- 分割线 -->
    <a-divider class="mt-0"/>

    <div class="flex justify-end">
      <a-space>
        <a-button @click="cancelBtnClick()">取消</a-button>
        <a-button type="primary" @click="okBtnClick()">确定</a-button>
      </a-space>
    </div>
  </a-spin>
</template>

<script setup>
import {getCurrentInstance, reactive, ref, watch} from 'vue'
import {addPostApi, getPostByPostIdApi, updatePostByPostIdApi} from "~/api/basic/post.js";

//全局实例
const {proxy} = getCurrentInstance()
//加载字典
const dicts = proxy.LoadDicts([proxy.DICT.commonNumberStatus])
//接收父组件参数
const props = defineProps({
  params: {
    type: Object,
    default: () => {
    }
  }
})
//父组件函数
const emits = defineEmits(['ok', 'cancel'])
//加载中...
const spinLoading = ref(false)
//表单ref
const formRef = ref(null)
//表单
const form = reactive({
  //岗位ID
  postId: null,
  //岗位名称
  postName: null,
  //岗位编码
  postCode: null,
  //排序值
  postSort: null,
  //岗位状态
  status: null,
  //备注
  remark: null
})
//表单校验规则
const rules = {
  postName: [{required: true, message: '岗位名称不能为空', trigger: 'submit'}],
  postCode: [{required: true, message: '岗位编码不能为空', trigger: 'submit'}],
  postSort: [{required: true, message: '排序值不能为空', trigger: 'submit'}],
}
//确定 -> 点击
const okBtnClick = () => {
  //表单验证
  formRef.value.validate((valid) => {
    if (valid) {
      return false
    }
    //添加
    if (props.params.operationType === proxy.operationType.add.type) {
      spinLoading.value = true
      addPostApi(form).then(() => {
        proxy.$msg.success(proxy.operationType.add.success)
        emits('ok')
      }).finally(() => {
        spinLoading.value = false
      })
    }
    //修改
    if (props.params.operationType === proxy.operationType.update.type) {
      spinLoading.value = true
      updatePostByPostIdApi(form).then(() => {
        proxy.$msg.success(proxy.operationType.update.success)
        emits('ok')
      }).finally(() => {
        spinLoading.value = false
      })
    }
  })
}
//取消 -> 点击
const cancelBtnClick = () => {
  emits('cancel')
}
//加载岗位详细信息
const loadPostInfo = (postId) => {
  spinLoading.value = true
  getPostByPostIdApi(postId).then(res => {
    //数据赋值
    if (res) {
      for (let key in res) {
        if (form.hasOwnProperty(key)) {
          form[key] = res[key]
        }
      }
    }
  }).finally(() => {
    spinLoading.value = false
  })
}
//监听参数变化
watch(() => props.params, (newVal, oldVal) => {
  //岗位ID
  if (props.params.postId) {
    //加载岗位信息
    loadPostInfo(props.params.postId)
  }
}, {deep: true, immediate: true})
</script>
<style scoped></style>
